#ifndef _H_BENCHMARK_
#define _H_BENCHMARK_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <inttypes.h>
#include <sys/time.h>
#include <pthread.h>
#include <values.h>

struct BenchmarkStat
{
	void		*user_data ;
	size_t		tidx ;
	pthread_t	tid ;
	uint64_t	count ;
	struct timeval	begin_timestamp ;
	struct timeval	end_timestamp ;
	struct timeval	diff_elapse ;
	double		qps ;
	double		delay ;
} ;

#define BEGIN_BENCHMARK(_bmstat_)	gettimeofday( & ((_bmstat_).begin_timestamp) , NULL );
#define END_BENCHMARK(_bmstat_)		gettimeofday( & ((_bmstat_).end_timestamp) , NULL );
#define DIFF_BENCHMARK(_bmstat_)	{ \
						(_bmstat_).diff_elapse.tv_sec = (_bmstat_).end_timestamp.tv_sec - (_bmstat_).begin_timestamp.tv_sec ; \
						(_bmstat_).diff_elapse.tv_usec = (_bmstat_).end_timestamp.tv_usec - (_bmstat_).begin_timestamp.tv_usec ; \
						while( (_bmstat_).diff_elapse.tv_usec < 0 ) \
						{ \
							(_bmstat_).diff_elapse.tv_usec += 1000000 ; \
							(_bmstat_).diff_elapse.tv_sec--; \
						} \
						(_bmstat_).qps = (double)((_bmstat_).count) / ( (double)((_bmstat_).diff_elapse.tv_sec) + (double)((_bmstat_).diff_elapse.tv_usec)/1000000 ) ; \
						(_bmstat_).delay = ( (double)((_bmstat_).diff_elapse.tv_sec) + (double)((_bmstat_).diff_elapse.tv_usec)/1000000 ) / (double)((_bmstat_).count) ; \
					} \
		

void TotalBenchmarkStat( struct BenchmarkStat *bmstats , size_t bmstats_count , uint64_t *total_count , double *total_elapse , double *total_qps , double *min_delay , double *avg_delay , double *max_delay );

#ifdef __cplusplus
}
#endif

#endif

